﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : System.Web.UI.Page
{
    ItemCategory currentCategory = new ItemCategory();
    List<ItemCategory> categories = new List<ItemCategory>();
    List<Item> items = new List<Item>();
    int currentPage = 0; //从0开始计算
    int cateId = 0;
    string searchStr = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        categories = InterfaceManager.DataManager.GetAllItemCategory();

        if (HttpContext.Current.Request["cate"] != null)
        {
            cateId = Convert.ToInt32(HttpUtility.UrlDecode(HttpContext.Current.Request["cate"].ToString()));
        }
        else
        {
            cateId = 0;
        }

        if (HttpContext.Current.Request["goodName"] != null)
        {
            searchStr = HttpUtility.UrlDecode(HttpContext.Current.Request["goodName"].ToString());
        }
        else
        {
            searchStr = "";
        }

        if (categories != null && categories.Count > cateId)
            currentCategory = categories[cateId];

        if (searchStr == null || searchStr == "")
        {
            items = InterfaceManager.DataManager.GetItemList(currentCategory, InterfaceManager.ItemNumOfOnePage, currentPage);
            
        }
        else
        {
            items = InterfaceManager.DataManager.GetItemListKeyWord(searchStr, InterfaceManager.ItemNumOfOnePage, currentPage);
        }

    }
    protected void PanelRight_Load(object sender, EventArgs e)
    {
        PlaceHolderRight.Controls.Clear();
        for (int i = 0; i < items.Count; i++)
        {
            control_item cc = Page.LoadControl("control/item.ascx") as control_item;

            cc.Name = items[i].goodName;
            cc.Image = "../images/"+items[i].picAddress;
            cc.CPrice = items[i].currentPrice.ToString();
            cc.RPrice = items[i].reservePrice.ToString();
            cc.Descript = items[i].description;
            cc.TargetURL = "~/bidItem.aspx?" + "itemId=" + items[i].id.ToString();
            PlaceHolderRight.Controls.Add(cc);
        }
    }
    protected void PanelLeft_Load(object sender, EventArgs e)
    {
        PlaceHolderLeft.Controls.Clear();
        for (int i = 0; i < categories.Count; i++)
        {
            control_cate cc = Page.LoadControl("control/cate.ascx") as control_cate;
            cc.Title = categories[i].category;
            cc.Content = "";
            cc.CateLink = "~/index.aspx?" + "cate=" + i.ToString();
            PlaceHolderLeft.Controls.Add(cc);
        }
    }

    public void btnReg_Click(object sender, EventArgs e)//应该改为bool
    {
        if (TextBox2.Text.ToString().CompareTo(TextBox3.Text.ToString()) != 0)
        {
            return;
        }
        int userId = InterfaceManager.DataManager.Register(TextBox1.Text.ToString(), TextBox2.Text.ToString(), TextBox4.Text.ToString());
        if (userId == -1)
        {
            Response.Write("<script>alert('注册失败')</script>");
            return;
        }
        Session["usrId"] = userId;
        Session["isLogin"] = true;
    }
    public void btnLogin_Click(object sender, EventArgs e)
    {

        int userId = InterfaceManager.DataManager.Login(TxtUserName.Text.ToString(), TxtUserPwd.Text.ToString());
        if (userId == -1)
        {
            Response.Write("<script>alert('登录信息错误')</script>");
            return;
        }
            
        Session["usrId"] = userId;
        Session["isLogin"] = true;
    }
    protected void inputB_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/index.aspx?goodName=" + inputT.Text.ToString());
    }
}
